module.exports = {
  
  requestGet : function(url){
    const accountInfo = null;
    // 获取当前运行环境
    let fullUrl=getApp().globalData.trialAPI+`${url}`
    try{
      const env = accountInfo.miniProgram.envVersion;
      switch (env) {
        case 'develop':
            fullUrl = getApp().globalData.devAPI+`${url}`
            console.log('当前运行环境：开发版');
            break;
        case 'trial':
            fullUrl = getApp().globalData.trialAPI+`${url}`
            console.log('当前运行环境：体验版');
            break;
        case 'release':
            fullUrl = getApp().globalData.prodAPI+`${url}`
            console.log('当前运行环境：正式版');
            break;
        default:
            fullUrl = getApp().globalData.trialAPI+`${url}`
            console.log('未知环境');
            break;
      }
    }catch{
    }
    console.log("url:  "+fullUrl);
    // let token = wx.getStorageSync('token') ? wx.getStorageSync('token')  : ''
    //(wx.showLoading)显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({ title: "数据请求中"  });
    return new Promise((resolve,reject)=>{
      wx.request({
        url: fullUrl,
        method:'GET',
        header: {
          'content-type': 'application/json', // 默认值
          // 'x-api-key': token,
        },
       success (res){
          if (res.data.code == 200) {
            resolve(res.data)
            wx.hideLoading()
          }else{
          //手动关闭loading提示框
            wx.hideLoading()
            wx.showToast({
              title: res.data.msg,
              icon:'none'
            })
            reject(res.data.message)
          }
        },
        fail(){
          wx.showToast({
            title: '接口请求错误',
            icon:'none'
          })
          reject('接口请求错误')
        }
      })
    })
  },
  requestPost : function(url,data){
    let fullUrl = getApp().globalData.baseAPI+`${url}`
    // let token = wx.getStorageSync('token') ? wx.getStorageSync('token')  : ''
    //(wx.showLoading)显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({ title: "数据请求中"  });
    return new Promise((resolve,reject)=>{
      wx.request({
        url: fullUrl,
        method:'POST',
        data:data,
        header: {
          'content-type': 'application/json', // 默认值
          // 'x-api-key': token,
        },
        success(res){
          if (res.data.code == 200) {
            resolve(res.data)
            wx.hideLoading()
          }else{
          //手动关闭loading提示框
            wx.hideLoading()
            wx.showToast({
              title: res.data.msg,
              icon:'none'
            })
            reject(res.data.message)
          }
        },
        fail(){
          wx.showToast({
            title: '接口请求错误',
            icon:'none'
          })
          reject('接口请求错误')
        }
      })
    })
  }
}